package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.PageDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.query.DishQuery;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.mapper.DishMapper;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author 马毅爽
* @description 针对表【dish(菜品)】的数据库操作Service实现
* @createDate 2023-11-03 09:41:33
*/
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService{

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = BeanUtil.copyProperties(dishDTO,Dish.class);
        save(dish);


        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public PageDTO<DishVO> queryPage(DishQuery dishQuery) {
        Page<DishVO> page = Page.of(dishQuery.getPage(),dishQuery.getPageSize());
        Page<DishVO> p = dishMapper.queryPage(page,dishQuery);
        return PageDTO.of(p);
    }

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //起售中的菜品不允许删除
        ids.forEach(id -> {
            if (Objects.equals(getById(id).getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        //关联套餐的菜品不能删除
        List<Long> setmealIds = setmealDishMapper.getSetmealIdByDishIds(ids);
        if (setmealIds != null && !setmealIds.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品后，关联的口味数据也需要删除掉
        removeBatchByIds(ids);
        dishFlavorMapper.deleteByDishId(ids);
    }

    @Override
    public DishVO queryById(Long id) {
        Dish dish = getById(id);
        DishVO dishVO = BeanUtil.copyProperties(dish,DishVO.class);
        dishVO.setFlavors(dishFlavorService.lambdaQuery().eq(DishFlavor::getDishId,id).list());
        return dishVO;
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = BeanUtil.copyProperties(dishDTO,Dish.class);
        updateById(dish);


        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });

        dishFlavorService.updateBatchById(flavors);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        updateById(dish);
    }

    @Override
    public List<DishVO> listByCategoryId(Long categoryId) {
        List<Dish> dishList = lambdaQuery()
                .eq(Dish::getCategoryId,categoryId)
                .eq(Dish::getStatus,StatusConstant.ENABLE)
                .list();

        List<DishVO> dishVOS = new ArrayList<>();
        dishList.forEach(dish -> {
            List<DishFlavor> dishFlavors = dishFlavorService.lambdaQuery()
                    .eq(DishFlavor::getDishId,dish.getId())
                    .list();
            DishVO dishVO = BeanUtil.copyProperties(dish,DishVO.class);
            dishVO.setFlavors(dishFlavors);
            dishVOS.add(dishVO);
        });

        return dishVOS;
    }
}




